﻿using Maticsoft.BLL;
using Maticsoft.DBUtility;
using Maticsoft.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Wuqi.Webdiyer;

public partial class WebPage_Index_YaoqingFanli : HelpBBasePage
{

    public Maticsoft.BLL.orderdetail detailbll = new Maticsoft.BLL.orderdetail();
    public List<Maticsoft.Model.orderdetail> detaillist = new List<Maticsoft.Model.orderdetail>();
    public Maticsoft.BLL.Flow flowbll = new Maticsoft.BLL.Flow();
    public Maticsoft.Model.Flow flowmodel = new Maticsoft.Model.Flow();

    public Maticsoft.BLL.Invite invite = new Maticsoft.BLL.Invite();
    public Maticsoft.Model.Invite invitemodel = new Maticsoft.Model.Invite();

    public Maticsoft.BLL.Order orderbll = new Maticsoft.BLL.Order();
    public Maticsoft.Model.Order ordermodel = new Maticsoft.Model.Order();
    public Maticsoft.BLL.Team teambll = new Maticsoft.BLL.Team();
    public Maticsoft.Model.Team teammodel = new Maticsoft.Model.Team();
    public string type = "Y";
    public Maticsoft.BLL.UserInfo userInfo = new Maticsoft.BLL.UserInfo();
    public Maticsoft.Model.UserInfo usermodel = new Maticsoft.Model.UserInfo();

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        this.fanLi();
    }

    private void fanLi()
    {
        int num;
        StringBuilder builder = new StringBuilder();
        builder.Append("<tr >");
        builder.Append("<th width='350' style='height: 42px'>项目</th>");
        builder.Append("<th width='150' style='height: 42px'>主动用户</th>");
        builder.Append("<th width='150' style='height: 42px'>被邀用户</th>");
        builder.Append("<th width='140' style='height: 42px'>邀买时间</th>");
        if (this.type == "N")
        {
            builder.Append("<th width='150' style='height: 42px'>操作</th>");
        }
        else
        {
            builder.Append("<th width='150' style='height: 42px'>操作员</th>");
        }
        builder.Append("</tr>");
        Maticsoft.BLL.Team team = new Maticsoft.BLL.Team();
        Maticsoft.Model.Team model = new Maticsoft.Model.Team();
        List<Maticsoft.Model.Invite> list = new List<Maticsoft.Model.Invite>();
        string fields = " Invite.Id,User_id,Admin_id,User_ip,Other_user_id,Other_user_ip,Team_id,Pay,Credit,Buy_time,Invite.Create_time";
        list = this.invite.SearchInvite(this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex, fields, "order by Buy_time desc", this.sqlWhere, out num);
        this.AspNetPager1.RecordCount = num;
        this.pages = this.AspNetPager1.CurrentPageIndex;
        int num2 = 0;
        StringBuilder builder2 = new StringBuilder();
        foreach (Maticsoft.Model.Invite invite in list)
        {
            if ((num2 % 2) != 0)
            {
                builder2.Append("<tr>");
            }
            else
            {
                builder2.Append("<tr class='alt'>");
            }
            num2++;
            model = team.GetModel(invite.Team_id);
            if (model != null)
            {
                builder2.Append(string.Concat(new object[] { "<td width='350' style='height: 42px'><a class='deal-title' href='../team.aspx?id=", model.Id, "' target='_blank'>", model.Title, "</td>" }));
            }
            else
            {
                this.detaillist = this.detailbll.Searchorderdetail(invite.Id);
                builder2.Append("<td width='350' style='height: 42px'>&nbsp;");
                int num3 = 0;
                foreach (Maticsoft.Model.orderdetail orderdetail in this.detaillist)
                {
                    num3++;
                    builder2.Append(string.Concat(new object[] { "(<a class='deal-title' href='../team.aspx?id=", orderdetail.Teamid, "' target='_blank'>", num3, ":" }));
                    builder2.Append((this.teambll.GetModel(Convert.ToInt32(orderdetail.Teamid)) == null) ? "" : string.Concat(new object[] { this.teambll.GetModel(Convert.ToInt32(orderdetail.Teamid)).Title, "<font style='color:red'>(", orderdetail.Num, "件)</font>" }));
                    builder2.Append("</a>)<br>");
                }
                builder2.Append("</td>");
            }
            foreach (Maticsoft.Model.UserInfo info in this.userInfo.GetModelList("Id=" + invite.User_id))
            {
                builder2.Append("<td width='150' style='height: 42px'>");
                builder2.Append("<font style='color:red'>" + info.Email + "</font><br>");
                builder2.Append(string.Concat(new object[] { "<a class='ajaxlink' href='/ajax/manage.aspx?action=userview&Id=", info.Id, "'>", info.Username, "</a><br>" }));
                builder2.Append(info.IP + "<br>");
                builder2.Append("<font style='color:red'>" + info.Mobile + "</font>");
                builder2.Append("</td>");
            }
            builder2.Append("<td width='150' style='height: 42px'>");
            int num1 = invite.Other_user_id;
            if (this.userInfo.GetModel(invite.Other_user_id) != null)
            {
                builder2.Append("<font style='color:red'>" + this.userInfo.GetModel(invite.Other_user_id).Email + "</font><br>");
                builder2.Append(string.Concat(new object[] { "<a class='ajaxlink' href='/ajax/manage.aspx?action=userview&Id=", this.userInfo.GetModel(invite.Other_user_id).Id, "'>", this.userInfo.GetModel(invite.Other_user_id).Username, "</a><br>" }));
                builder2.Append(this.userInfo.GetModel(invite.Other_user_id).IP + "<br>");
                builder2.Append("<font style='color:red'>" + this.userInfo.GetModel(invite.Other_user_id).Mobile + "</font>");
                builder2.Append("</td>");
                builder2.Append("<td width='140' style='height: 42px'>" + invite.Create_time + "</td>");
                if (this.type == "Y")
                {
                    builder2.Append(string.Concat(new object[] { "<td width='150' style='height: 42px'><a href='Index-YaoqingFanli.aspx?confir=", invite.Id, "'>确认</a> | <a href='Index-YaoqingFanli.aspx?del=", invite.Id, "'>取消</a></td>" }));
                }
                else
                {
                    builder2.Append("<td width='150' style='height: 42px'>" + base.AsUser.Username + "</td>");
                }
                builder2.Append("</tr>");
            }
        }
        this.Literal1.Text = builder.ToString();
        this.Literal2.Text = builder2.ToString();
    }

    public void GetFanli(int id)
    {
        this.invitemodel = this.invite.GetModel(id);
        this.usermodel = this.userInfo.GetModel(this.invitemodel.User_id);
        this.ordermodel = this.orderbll.GetSuccessorder(this.invitemodel.Other_user_id);
        if (this.ordermodel != null)
        {
            this.teammodel = this.teambll.GetModel(this.ordermodel.Team_id);
            if (this.teammodel != null)
            {
                if (this.teammodel.Bonus > 0)
                {
                    this.userInfo.Updatemoney(this.invitemodel.User_id, this.usermodel.Money, decimal.Parse(this.teammodel.Bonus.ToString()));
                    this.invitemodel.Admin_id = base.AsUser.Id;
                    this.invitemodel.Credit = this.teammodel.Bonus;
                    this.invite.Update(this.invitemodel);
                    this.flowmodel.User_id = this.invitemodel.User_id;
                    this.flowmodel.Detail_id = this.ordermodel.Pay_id;
                    this.flowmodel.Direction = "income";
                    this.flowmodel.Money = this.invitemodel.Credit;
                    this.flowmodel.Action = "invite";
                    this.flowmodel.Create_time = DateTime.Now;
                    this.flowbll.Add(this.flowmodel);
                    this.fanLi();
                    base.SetSuccess("友情提示：返利成功");
                }
                else
                {
                    this.SetError("对不起，被邀请人购买的项目没有邀请返利");
                }
            }
            else
            {
                DataSet set = DbHelperSQL.Query("select top 1 max(Bonus) as bon,Id from Team where Id in (select Teamid from orderdetail where Order_id=" + this.ordermodel.Id + ") group by Id order by max(Bonus) desc");
                if (set.Tables[0].Rows.Count > 0)
                {
                    this.userInfo.Updatemoney(this.invitemodel.User_id, this.usermodel.Money, decimal.Parse(set.Tables[0].Rows[0]["bon"].ToString()));
                    this.invitemodel.Admin_id = base.AsUser.Id;
                    this.invitemodel.Credit = int.Parse(set.Tables[0].Rows[0]["bon"].ToString());
                    this.invite.Update(this.invitemodel);
                    this.flowmodel.User_id = this.invitemodel.User_id;
                    this.flowmodel.Detail_id = this.ordermodel.Pay_id;
                    this.flowmodel.Direction = "income";
                    this.flowmodel.Money = this.invitemodel.Credit;
                    this.flowmodel.Action = "invite";
                    this.flowmodel.Create_time = DateTime.Now;
                    this.flowbll.Add(this.flowmodel);
                    this.fanLi();
                    base.SetSuccess("友情提示：返利成功");
                }
                else
                {
                    this.SetError("友情提示：返利失败");
                }
            }
        }
    }

    public string GetStyle(string s, string style)
    {
        string str = "";
        if (s == style)
        {
            str = "class='current'";
        }
        return str;
    }

    public void Getwhere(string type, string email)
    {
        string str = "";
        if (email != "")
        {
            str = " and email like '" + email + "%'";
        }
        if (type == "Y")
        {
            this.sqlWhere = " Pay='" + type + "' and Credit=0" + str;
        }
        else if (type == "P")
        {
            this.sqlWhere = " Credit>0" + str;
        }
        else if (type == "C")
        {
            this.sqlWhere = " Pay='" + type + "'" + str;
        }
    }

    public void NoRule(int id)
    {
        this.invitemodel = this.invite.GetModel(id);
        this.invitemodel.Pay = "C";
        this.invite.Update(this.invitemodel);
        this.fanLi();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.HeaderControl1.Select = "index";
        if (base.Request["confir"] != null)
        {
            this.GetFanli(Convert.ToInt32(base.Request["confir"]));
        }
        if (base.Request["del"] != null)
        {
            this.NoRule(Convert.ToInt32(base.Request["del"]));
        }
        if (base.Request.QueryString["s"] != null)
        {
            this.type = base.Request.QueryString["s"].ToString();
            this.Getwhere(this.type, "");
            this.fanLi();
        }
        else
        {
            this.fanLi();
        }
        if (base.Request["btnselect"] == "筛选")
        {
            this.Getwhere(this.type, base.Request["memail"]);
            this.fanLi();
        }
    }

    public int pages
    {
        get
        {
            if (this.ViewState["pages"] != null)
            {
                return Convert.ToInt32(this.ViewState["pages"].ToString());
            }
            return 1;
        }
        set
        {
            this.ViewState["pages"] = value;
        }
    }

    public string sqlWhere
    {
        get
        {
            if (this.ViewState["sqlWhere"] != null)
            {
                return this.ViewState["sqlWhere"].ToString();
            }
            return " Pay='Y' and Credit=0";
        }
        set
        {
            this.ViewState["sqlWhere"] = value;
        }
    }

}

